package sword_offer;

/**
 * @author Synhard
 * @version 1.0
 * @class Code69
 * @description 剑指 Offer 60. n个骰子的点数
 * 把n个骰子扔在地上，所有骰子朝上一面的点数之和为s。输入n，打印出s的所有可能的值出现的概率。
 *
 *  
 *
 * 你需要用一个浮点数数组返回答案，其中第 i 个元素代表这 n 个骰子所能掷出的点数集合中第 i 小的那个的概率。
 *
 *  
 *
 * 示例 1:
 *
 * 输入: 1
 * 输出: [0.16667,0.16667,0.16667,0.16667,0.16667,0.16667]
 * 示例 2:
 *
 * 输入: 2
 * 输出: [0.02778,0.05556,0.08333,0.11111,0.13889,0.16667,0.13889,0.11111,0.08333,0.05556,0.02778]
 *
 * @tel 13001321080
 * @email 823436512@qq.com
 * @date 2021-07-18 8:31
 */
public class Code69 {
    public static void main(String[] args) {

    }

    public double[] dicesProbability(int n) {
        int[][] matrix = new int[n + 1][6 * n + 1];
        for (int i = 1; i <= 6; i++) {
            matrix[1][i] = 1;
        }

        for (int i = 2; i < matrix.length; i++) {
            for (int j = i; j <= i * 6; j++) {
                for (int k = j - 1; k >= i - 1 && k >= j - 6; k--) {
                    matrix[i][j] += matrix[i - 1][k];
                }
            }
        }

        double[] ans = new double[6 * n - n + 1];
        for(int i = n; i <= 6 * n; i++) {
            ans[i - n] = ((double)matrix[n][i]) / (Math.pow(6,n));
        }
        return ans;
    }
}
/*
dp[i][j] ，表示投掷完 i 枚骰子后，点数 j 的出现次数。
for (第n枚骰子的点数 i = 1; i <= 6; i ++) {
    dp[n][j] += dp[n-1][j - i]
}
 */